package proteus.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;


import proteus.excepcion.DAOExcepcion;
import proteus.modelo.Local;
import proteus.util.ConexionBD;

public class LocalDAO extends BaseDAO{
	
public int validar_nombre_local(String ParamLocal,int CodDueno) throws DAOExcepcion{
		
		int intReturn=0;
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		
		String query = "";
		query +="SELECT COUNT(*) AS CANTIDAD FROM local WHERE ";
		query +="NoLocal=? and CoPersona=? ";
		
		
		try{
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			
			
			stmt.setString(1,ParamLocal);
			stmt.setInt(2,CodDueno);
			

			rs = stmt.executeQuery();
			while (rs.next()) {
			intReturn=rs.getInt("CANTIDAD");	
			}	
			}
			catch (SQLException e)
			{
			throw new DAOExcepcion(e.getMessage());
			}
			finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
			}	
			return intReturn;
		
	}
	
	
	public void insertar(Local loc)throws DAOExcepcion{
		
		String query = "";
		query +="INSERT INTO local(NoLocal,TxDireccion,NoDistrito,TxDireccionGoogle,";
		query +="NuTelefono,CoPersona)";
		query +="VALUES";
		query +="(?,?,?,?,?,?)";
		
		Connection con = null;
		PreparedStatement stmt = null;

		try
		{
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1,loc.getNoLocal());
			stmt.setString(2, loc.getTxDireccion());
			stmt.setString(3,loc.getNoDistrito());
			stmt.setString(4,loc.getTxDireccionGoogle() );
			stmt.setString(5,loc.getNuTelefono());

			
			//stmt.setInt(6,loc.getPersona().getCoPersona());
			stmt.setInt(6,loc.getoDueno().getCoPersona());
			
			
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo insertar");
			}			
		}
		catch(SQLException e)
		{
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		}
		finally
		{			
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}


	}

	
	public Collection<Local> listarLocales() throws DAOExcepcion {
		String query;
		Collection<Local> s = new ArrayList<Local>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		
		try {
			con = ConexionBD.obtenerConexion();
			query = "select * from local";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			
			while (rs.next()) {
					
					Local lo= new Local();
					lo.setCoLocal(rs.getInt("CoLocal"));
					lo.setNoLocal(rs.getString("NoLocal"));
					lo.setTxDireccion(rs.getString("TxDireccion"));
					lo.setNoDistrito(rs.getString("NoDistrito"));
					lo.setTxDireccionGoogle(rs.getString("TxDireccionGoogle"));
					lo.setNuTelefono(rs.getString("NuTelefono"));
					
					s.add(lo);
					
				} 
			}catch (SQLException e) {
				System.err.println(e.getMessage());
				throw new DAOExcepcion(e.getMessage());
			} finally {
				this.cerrarStatement(stmt);
				this.cerrarConexion(con);
			}
			return s;	
	}
	
	
	
	
}
